package com.mayi.dao.impl;

import com.mayi.dao.DruidDao;
import com.mayi.dao.IUserDao;
import com.mayi.pojo.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther: yacong_liu
 * @Date: 2019/7/3
 * @version: 1.0
 */
public class UserDaoImpl implements IUserDao {
    @Override
    public int save(User user) {
        StringBuffer sql = new StringBuffer("insert into t_user(number,name,age) values( ");
        sql.append("'" + user.getNumber() + "',")
                .append("'" + user.getName() + "',")
                .append(user.getAge()).append(" )");

        return DruidDao.getInstance().excuteSQL(sql.toString());
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from t_user where id = " + id + "";
        return DruidDao.getInstance().excuteSQL(sql);
    }

    @Override
    public int update(User user) {
        String sql = "update t_user user set user.number = '" + user.getNumber() + "',user.name= '" + user.getName()
                + "',user.age = " + user.getAge() + " where user.id = " + user.getId();
        return DruidDao.getInstance().excuteSQL(sql);
    }

    @Override
    public User findById(Integer id) {
        String sql = "select id,number,name,age from t_user user where user.id = " + id;
        /*ResultSet resultSet = DruidDao.getInstance().excuteQuery(sql);
        try {
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setNumber(resultSet.getString("number"));
                user.setName(resultSet.getString("name"));
                user.setAge(resultSet.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            System.out.println(this.getClass().getName() + "_findById 执行SQL is ERROR! sql: " + sql);
            e.printStackTrace();
        }*/
        return null;
    }

    @Override
    public List<User> findAll() {
        String sql = "select id,number,name,age from t_user user ";
        List<Map<String, Object>> rs = DruidDao.getInstance().excuteQuery(sql);
        List<User> list = new ArrayList<User>();
        Iterator<Map<String, Object>> iterator = rs.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            User user = new User();
            user.setId((Integer) map.get("id"));
            user.setNumber((String) map.get("number"));
            user.setName((String) map.get("name"));
            user.setAge((Integer) map.get("age"));

            list.add(user);
        }

        System.out.println(rs);

        /*List<User> list = new ArrayList<User>();
        try {
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setNumber(rs.getString("number"));
                user.setName(rs.getString("name"));
                user.setAge(rs.getInt("age"));

                list.add(user);
            }
        } catch (SQLException e) {
            System.out.println(this.getClass().getName() + "_findAll 执行SQL is ERROR! sql: " + sql);
            e.printStackTrace();
        }*/

        return list;
    }
}
